var self,goodsid,title;
var shares=null,bhref=false;
var Intent=null,File=null,Uri=null,main=null;
document.addEventListener("plusready",function(){
	$('.qq').on('tap',function(){shareChange(0);})
	$('.weixin').on('tap',function(){shareChange(1);})
	$('.friends').on('tap',function(){shareChange(2);})
	$('.micro-blog').on('tap',function(){shareChange(3);})
	updateSerivces(); 
	if(plus.os.name=="Android"){
		Intent = plus.android.importClass("android.content.Intent");
		File = plus.android.importClass("java.io.File");
		Uri = plus.android.importClass("android.net.Uri");
		main = plus.android.runtimeMainActivity();
	} 
},false);

/**
 * 更新分享服务
 */
function updateSerivces(){
	plus.share.getServices( function(s){
		shares={};
		for(var i in s){
			var t=s[i];
			shares[t.id]=t;
		}
	}, function(e){
		plus.nativeUI.toast( "获取分享服务列表失败："+e.message );
	} );
}
/**
 * 弹出分享平台选择
 */ 
function shareChange(i){
	plus.nativeUI.showWaiting('申请授权中...');
$('#picture').hide();
//	var uid = plus.storage.getItem('phone');
//	uid = uid?uid:'';
	var ids=[{id:"qq"},{id:"weixin",ex:"WXSceneSession"},{id:"weixin",ex:"WXSceneTimeline"},{id:"sinaweibo"}];
 	var s = shares[ids[i].id];
// 	console.log(JSON.stringify(s))
	if ( s.authenticated ) {
		plus.nativeUI.closeWaiting();
		shareMessage(shares[ids[i].id],ids[i].ex);
	} else {
		s.authorize( function(){
				shareMessage(shares[ids[i].id],ids[i].ex);
			},function(e){
			plus.nativeUI.alert( "认证授权失败"+e.message,null,'提示' );
			plus.nativeUI.closeWaiting();
		});
	}
}  
	/**
   * 发送分享消息
   * @param {plus.share.ShareService} s
   */
function shareMessage(s,ex){ 
	plus.nativeUI.closeWaiting();
	var methods = ws.id;   
	var methods = methods.substr(0,(methods.indexOf('.')));
	$('.mui-backdrop').remove(); 
	console.log(cid+'--'+tid+'--'+member_id);
	if(!cid){
		cid = '';
	}
	if(!tid){
		tid = '';
	}
	if(!member_id){
		member_id = '';
	}
//	if(!goods_id){
//		goods_id = '';
//	} 
	console.log(apiRoot+'?act=share&op=friends_details&c_id='+cid+'&member_id='+member_id+'&t_id='+tid);  
	var msg={title : '小伙伴' , extra : {scene:ex}  , content:"亲，您的好友向你推荐一个超赞的校园平台App！",href:apiRoot+'?act=share&op=friends_details&c_id='+cid+'&member_id='+member_id+'&t_id='+tid,thumbs:['/images/17.png']};
	s.send( msg, function(){
		plus.nativeUI.toast( "分享成功");
		//分享成功后分享次数增加
		$.ajax({
			type:"get",
			url:circle + "/index.php?act=theme&op=share_friend",
			data:{
				t_id:tid,
				c_id:cid,
				u_id:member_id
			},
			success:function(data){
				console.log(JSON.stringify(data));
				if(data > 0){
					console.log(f_id); 
					if(f_id == 1){ 
						var likeCount = parseInt($('.sharecount').html()) + 1;  
						$('.sharecount').html(likeCount);
					}else{
						var likeCount = parseInt($('.share'+share_k ).find('.share_count').html()) + 1;
						$('.share'+share_k ).find('.share_count').html(likeCount);
					}
				}
			},
			error:function(e){
			 	
			}
		});
		$(".alert").hide();
		$(".alert-back").hide();
	}, function(e){  
		plus.nativeUI.toast( "分享失败" );
		$(".alert").hide();
		$(".alert-back").hide();
	}); 
}
